package com.common.core.domain;

import java.io.Serializable;
import java.util.HashMap;

import com.common.core.constant.Constants;
import com.common.core.constant.HttpStatus;
import com.common.core.utils.StringUtils;

/**
 * 响应信息主体
 *
 *
 */
public class CodeMessage<T> extends HashMap implements Serializable
{
    private static final long serialVersionUID = 1L;

    /** 成功 */
    public static final int SUCCESS = Constants.SUCCESS;

    /** 失败 */
    public static final int FAIL = Constants.FAIL;

    private int code;

    private String msg;

    private T data;


    public static <T> CodeMessage<T> ok()
    {
        return restResult(null, SUCCESS, null);
    }

    public static <T> CodeMessage<T> ok(T data)
    {
        return restResult(data, SUCCESS, null);
    }

    public static <T> CodeMessage<T> ok(T data, String msg)
    {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> CodeMessage<T> fail()
    {
        return restResult(null, FAIL, null);
    }

    public static <T> CodeMessage<T> fail(String msg)
    {
        return restResult(null, FAIL, msg);
    }

    public static <T> CodeMessage<T> fail(T data)
    {
        return restResult(data, FAIL, null);
    }

    public static <T> CodeMessage<T> fail(T data, String msg)
    {
        return restResult(data, FAIL, msg);
    }

    public static <T> CodeMessage<T> fail(int code, String msg)
    {
        return restResult(null, code, msg);
    }

    private static <T> CodeMessage<T> restResult(T data, int code, String msg)
    {
        CodeMessage<T> apiResult = new CodeMessage<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public CodeMessage() {
    }

    public int getCode()
    {
        return code;
    }

    public void setCode(int code)
    {
        this.code = code;
    }

    public String getMsg()
    {
        return msg;
    }

    public void setMsg(String msg)
    {
        this.msg = msg;
    }

    public T getData()
    {
        return data;
    }

    public void setData(T data)
    {
        this.data = data;
    }


    /**
     * 初始化一个新创建的 CodeMessage 对象
     *
     * @param code 状态码
     * @param msg 返回内容
     * @param data 数据对象
     */
    public CodeMessage(int code, String msg, T data)
    {
        this.code = code;
        this.msg = msg;

        if (StringUtils.isNotNull(data))
        {
            this.data = data;
        }
    }


    /**
     * 返回成功消息
     *
     * @return 成功消息
     */
    public static CodeMessage success()
    {
        return CodeMessage.success("操作成功");
    }

    /**
     * 返回成功数据
     *
     * @return 成功消息
     */
    public static CodeMessage success(Object data)
    {
        return CodeMessage.success("操作成功", data);
    }

    /**
     * 返回成功消息
     *
     * @param msg 返回内容
     * @return 成功消息
     */
    public static CodeMessage success(String msg)
    {
        return CodeMessage.success(msg, null);
    }

    /**
     * 返回成功消息
     *
     * @param msg 返回内容
     * @param data 数据对象
     * @return 成功消息
     */
    public static CodeMessage success(String msg, Object data)
    {
        return new CodeMessage(HttpStatus.SUCCESS, msg, data);
    }

    /**
     * 返回错误消息
     *
     * @return
     */
    public static CodeMessage error()
    {
        return CodeMessage.error("操作失败");
    }

    /**
     * 返回错误消息
     *
     * @param msg 返回内容
     * @return 警告消息
     */
    public static CodeMessage error(String msg)
    {
        return CodeMessage.error(msg, null);
    }

    /**
     * 返回错误消息
     *
     * @param msg 返回内容
     * @param data 数据对象
     * @return 警告消息
     */
    public static CodeMessage error(String msg, Object data)
    {
        return new CodeMessage(HttpStatus.ERROR, msg, data);
    }

    /**
     * 返回错误消息
     *
     * @param code 状态码
     * @param msg 返回内容
     * @return 警告消息
     */
    public static CodeMessage error(int code, String msg)
    {
        return new CodeMessage(code, msg, null);
    }
}
